package com.xkdx.guangguang.fragment.web;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.fragment.my.pay.PayFragment;

@SuppressLint("SetJavaScriptEnabled")
public class WebFragment extends Fragment {
	private static String TAG = "WebFragment";

	WebView webView;
	String www;
	TextView title;
	ProgressBar pb;
	Button back;
	private int CANCLE_PROGRESSBAR = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		PayFragment.isBack=true;
		www = getArguments().getString("url");

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.web, container, false);
		title = (TextView) view.findViewById(R.id.title);
		pb = (ProgressBar) view.findViewById(R.id.progressBar);
		back = (Button) view.findViewById(R.id.web_back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentManager fm = getFragmentManager();
				fm.popBackStack();

			}
		});
		String name = getArguments().getString("title");
		if (name != null && !title.equals("")) {
			title.setText(name);
		}

		webView = (WebView) view.findViewById(R.id.webView);
		webView.loadUrl(www);
		((ManagerActivity) getActivity()).setWebView(webView);
		webView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {

				// Activity和Webview根据加载程度决定进度条的进度大小

				// 当加载到100%的时候 进度条自动消失
				pb.setVisibility(View.VISIBLE);
				// Log.i(TAG, "progress-->" + progress);
				if (progress == 100) {
					h.sendEmptyMessage(CANCLE_PROGRESSBAR);
				}
			}
		});
		WebSettings setting = webView.getSettings();
		webView.setWebViewClient(new webViewClient());
		setting.setBuiltInZoomControls(true);
		//webView设置自适应
		setting.setUseWideViewPort(true);
		setting.setLoadWithOverviewMode(true);
		setting.setJavaScriptEnabled(true);
		webView.requestFocus(); //弹出输入法
		pb.setVisibility(View.VISIBLE);
		return view;
	}

	Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if(getActivity()==null){
				return;
			}
			if (msg.what == CANCLE_PROGRESSBAR) {
				pb.setVisibility(View.GONE);
			}
		}
	};

	class webViewClient extends WebViewClient {

		// 重写shouldOverrideUrlLoading方法，使点击链接后不使用其他的浏览器打开。

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {

			view.loadUrl(url);

			// 如果不需要其他对点击链接事件的处理返回true，否则返回false

			return true;

		}
	}

}
